//-------------------------------------------------------------
#include "thread.h"
#include <chrono>
#include <vector>
namespace tt01
{

    //测试mutex 
learnco::Mutex global_mutex;
long count = 0;
    void fun() {

        global_mutex.lock();
        for(int i=0; i<10000000; ++i){ 
            count++;
        }
        global_mutex.unlock();
    }

    void test_mutex()
    {
 auto start = std::chrono::system_clock::now();
        std::vector<learnco::Thread::ptr> thrs;
        for(int i=0; i<24; i++) {
            learnco::Thread::ptr thr(new learnco::Thread(&fun, "main"));
            thrs.push_back(thr);
        }   
        for(size_t i = 0; i < thrs.size(); ++i) {
            thrs[i]->join();
        }
auto end = std::chrono::system_clock::now();
        std::cout << "result " << count ;
        std::cout << "  for-loop, cost ";
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
    }
} //namespace 


//-------------------------------------------------------------
#include "thread.h"
#include <chrono>
#include <vector>
namespace tt02
{
    //测试mutex  RAII
long count = 0;
learnco::Mutex global_mutex;
    void fun() {
learnco::Mutex::RaiiImpl raii_mutex(global_mutex);
        for(int i=0; i<10000000; ++i){ 
            count++;
        }
    }

    void test_mutex_raii()
    {
 auto start = std::chrono::system_clock::now();
        std::vector<learnco::Thread::ptr> thrs;
        for(int i=0; i<24; i++) {
            learnco::Thread::ptr thr(new learnco::Thread(&fun, "main"));
            thrs.push_back(thr);
        }   
        for(size_t i = 0; i < thrs.size(); ++i) {
            thrs[i]->join();
        }
auto end = std::chrono::system_clock::now();
        std::cout << "result " << count ;
        std::cout << "  for-loop, cost ";
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
    }
} //namespace


//-------------------------------------------------------------
#include "thread.h"
#include <chrono>
#include <vector>
namespace tt03
{
    //测试 读写锁
long count = 0;
learnco::RWMutex global_rwmutex;
    void fun() {

        global_rwmutex.wrlock();
        for(int i=0; i<10000000; ++i){ 
            count++;
        }
        global_rwmutex.unlock();
    }

    void test_rwmutex()
    {
 auto start = std::chrono::system_clock::now();
        std::vector<learnco::Thread::ptr> thrs;
        for(int i=0; i<24; i++) {
            learnco::Thread::ptr thr(new learnco::Thread(&fun, "main"));
            thrs.push_back(thr);
        }   
        for(size_t i = 0; i < thrs.size(); ++i) {
            thrs[i]->join();
        }
auto end = std::chrono::system_clock::now();
        std::cout << "result " << count ;
        std::cout << "  for-loop, cost ";
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
    }
} //namespace



int main()
{
    tt01::test_mutex();	
    tt02::test_mutex_raii();
    tt03::test_rwmutex();
    return 0;
}